package cn.edu.nju.software.android.layoutHelper;

import java.text.SimpleDateFormat;
import java.util.Date;

import me.maxwin.view.IListviewCallback;
import me.maxwin.view.XListView;
import me.maxwin.view.XListView.IXListViewListener;
import cn.edu.nju.software.android.activity.DetailedUserActivity;
import cn.edu.nju.software.android.activity.MainActivity;
import cn.edu.nju.software.android.activity.R;
import cn.edu.nju.software.android.adapter.UserListViewAdapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;

public class FriendsLayoutHelper extends LayoutHelper implements
IXListViewListener, IListviewCallback {
	private UserListViewAdapter followedByMeAdapter;
	private UserListViewAdapter followingMeAdapter;
	private Handler mHandler = new Handler();

	private TabHost tabHost;
	private XListView followedByMeList;
	private XListView followingMeList;
	private String tabTag_followedByMe;
	private String tabTag_followingMe;

	public FriendsLayoutHelper(Context context) {
		super(context);
	}

	@Override
	public void initialize(View view) {
		followedByMeList = (XListView) view.findViewById(R.id.followed_by_me_list);
		followingMeList = (XListView) view.findViewById(R.id.following_me_list);
		followingMeList.setXListViewListener(this);
		followingMeList.setPullLoadEnable(false);
		followingMeList.setPullRefreshEnable(false);
		followedByMeList.setXListViewListener(this);
		followedByMeList.setPullLoadEnable(false);
		followedByMeList.setPullRefreshEnable(false);
		followingMeList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent = new Intent(context, DetailedUserActivity.class);
				intent.putExtra("user", followingMeAdapter.getItem(position-1));
				intent.putExtra("parent", MainActivity.class);
				context.startActivity(intent);
			}
		});
		followedByMeList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				Intent intent = new Intent(context, DetailedUserActivity.class);
				intent.putExtra("user", followedByMeAdapter.getItem(position-1));
				intent.putExtra("parent", MainActivity.class);
				context.startActivity(intent);
			}
		});
		setupTabInfomation(view);
	}

	private void setupTabInfomation(View view) {
		tabTag_followedByMe = context.getResources().getString(
				R.string.tab_tag_followedByMe);
		tabTag_followingMe = context.getResources().getString(
				R.string.tab_tag_followingMe);
		tabHost = (TabHost) view.findViewById(android.R.id.tabhost);
		tabHost.setup();
		tabHost.addTab(tabHost
				.newTabSpec(tabTag_followedByMe)
				.setIndicator(
						context.getResources().getString(
								R.string.tab_text_followedByMe))
								.setContent(R.id.followed_by_me_list));
		tabHost.addTab(tabHost
				.newTabSpec(tabTag_followingMe)
				.setIndicator(
						context.getResources().getString(
								R.string.tab_text_followingMe))
								.setContent(R.id.following_me_list));
		followedByMeAdapter = new UserListViewAdapter(context, this,
				MainActivity.user, UserListViewAdapter.FRIEND);
		followingMeAdapter = new UserListViewAdapter(context, this,
				MainActivity.user, UserListViewAdapter.FOLLOWER);
		followedByMeList.setAdapter(followedByMeAdapter);
		followingMeList.setAdapter(followingMeAdapter);
	}

	@Override
	public void onRefresh() {
	}

	@Override
	public void onLoadMore() {
		String currentTabTag = tabHost.getCurrentTabTag();
		if (currentTabTag.equals(tabTag_followedByMe)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					followedByMeAdapter.getMoreUsers(MainActivity.uid);
				}
			}, 2000);
		}
		if (currentTabTag.equals(tabTag_followingMe)) {
			mHandler.postDelayed(new Runnable() {
				@Override
				public void run() {
					followingMeAdapter.getMoreUsers(MainActivity.uid);
				}
			}, 2000);
		}
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public void onComplete() {
		String currentTabTag = tabHost.getCurrentTabTag();
		Date timeNow = new Date();
		SimpleDateFormat format = new SimpleDateFormat("HH:mm");
		String time = format.format(timeNow);
		if (currentTabTag.equals(tabTag_followedByMe)) {
			followedByMeList.stopLoadMore();
			followedByMeList.setRefreshTime(time);
		}
		if (currentTabTag.equals(tabTag_followingMe)) {
			followingMeList.stopLoadMore();
			followingMeList.setRefreshTime(time);
		}
	}

	@Override
	public void disableLoadFunction() {
		String currentTabTag = tabHost.getCurrentTabTag();
		if (currentTabTag.equals(tabTag_followedByMe)) {
			followedByMeList.setPullLoadEnable(false);
		}
		if (currentTabTag.equals(tabTag_followingMe)) {
			followingMeList.setPullLoadEnable(false);
		}
	}

	@Override
	public void enableLoadFunction() {
		String currentTabTag = tabHost.getCurrentTabTag();
		if (currentTabTag.equals(tabTag_followedByMe)) {
			followedByMeList.setPullLoadEnable(true);
		}
		if (currentTabTag.equals(tabTag_followingMe)) {
			followingMeList.setPullLoadEnable(true);
		}
	}

	@Override
	public void resetToTopRow() {
		// TODO Auto-generated method stub

	}

}
